/**
 * Created by mac on 2017/5/31.
 */
import java.io.*;
import static oracle.jrockit.jfr.events.Bits.intValue;

public class IO2 {
    public static void dump(InputStream src, OutputStream dest, String[] args)
            throws IOException {
        try (InputStream input = src; OutputStream output = dest) {
            byte[] data1 = new byte[100];
            byte[] data2 = new byte[8];
            byte[] data = new byte[100];
            //char[] testINT = new char[100];

            if(args[0].equals("-tx")) {
                int length1 = input.read(data1);                     //读取100Byte
                for(int i=0; i<length1; i++) {
                    if(data1[i] != 0)
                    data2 = To2(data1[i]);
                    output.write(data2, 0, 8);
                }
            }

            else if(args[0].equals("-xt")) {
                for(int j=0; input.read(data2) != -1; j++) {
                        data[j] = To10(data2);
                        //testINT[j] = (char)data[j];
                        //System.out.println(data[j]);
                }
                //for(int xx=0; xx<data.length&&data[xx] != 0; xx++) {
                //    System.out.println(data[xx]);
                //}
                int length2 = 0;
                while ((length2 = input.read(data)) != -1) {
                    output.write(data, 0, length2);
                }
        }
            else System.out.println("wrong!");

        }
    }

    public static byte[] To2(byte input) {
        byte[] test1 = new byte[8];
        int temp,temp2;
        String s,s1;
        String temps = "";
        //for(int k=0; k<test1.length; k++) {
            //temp = (int)input;                    //把byte型转换为整形
            temp = intValue(input);                 //把byte型转换为整形(方法二)

            s = Integer.toBinaryString(temp);       //用Integer方法将整形转换为字符串型二进制
            //System.out.println(s);                //检测
            /*
            for(int i=0; i<8; i++) {
                if(i<(8-s.length()))temps+='0';
            }
            temps+=s;
            */
            temp2 = Integer.parseInt(s);            //将二进制字符串转回整型
             s1 = String.format("%08d", temp2);      //将整形二进制数补八位并转换为字符串
             System.out.println(s1);
             test1 = s1.getBytes();                  //将字符串转为Byte数组
        //}
            return test1;
    }

    public static byte To10(byte[] input) {
        String s = new String(input);               //将二进制字符数组传唤为字符串
        byte temp = Byte.valueOf(s, 2);             //将字符串转换为十进制字符
        temp -= '0';
        return temp;
    }
}
